<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="风场" prop="windId">
        <el-select v-model="queryParams.windId" placeholder="请选择风场" @change="handleChange">
          <el-option
            v-for="item in windList"
            :key="item.id"
            :label="item.windName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="机组" prop="setId">
        <el-select v-model="queryParams.setId" placeholder="请选择机组" @change="handleSetChange">
          <el-option
            v-for="item in setList"
            :key="item.id"
            :label="item.setName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备" prop="equipmentId">
        <el-select v-model="queryParams.equipmentId" placeholder="请选择设备" @change="handleQuery">
          <el-option
            v-for="item in equipmentList"
            :key="item.id"
            :label="item.equipmentName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:repairs:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:repairs:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:repairs:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="维修等级" align="center" prop="repairsLevel" />
      <el-table-column label="维修类别" align="center" prop="repairsType" />
      <el-table-column label="工作票编号" align="center" prop="ticketNumber" width="120" />
      <el-table-column label="维修动作" align="center" prop="repairsAction" />
      <el-table-column label="维修时间" align="center" prop="repairsTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下次维修时间" align="center" prop="nextRepairsTime" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.nextRepairsTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修费用" align="center" prop="repairsFee" />
      <el-table-column label="备件消耗" align="center" prop="spareParts" />
      <el-table-column label="维修结果" align="center" prop="repairsResult" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="维修员" align="center" prop="repairsPerson" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:repairs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:repairs:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="风场" prop="windId">
          <el-select v-model="form.windId" placeholder="请选择风场" @change="handleChange" style="width: 100%">
            <el-option
              v-for="item in windList"
              :key="item.id"
              :label="item.windName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="机组" prop="setId">
          <el-select v-model="form.setId" placeholder="请选择机组" @change="handleSetChange" style="width: 100%">
            <el-option
              v-for="item in setList"
              :key="item.id"
              :label="item.setName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备" prop="equipmentId">
          <el-select v-model="form.equipmentId" placeholder="请选择设备" style="width: 100%">
            <el-option
              v-for="item in equipmentList"
              :key="item.id"
              :label="item.equipmentName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="维修等级" prop="repairsLevel">
          <el-input v-model="form.repairsLevel" placeholder="请输入维修等级" />
        </el-form-item>
        <el-form-item label="工作票编号" prop="ticketNumber">
          <el-input v-model="form.ticketNumber" placeholder="请输入工作票编号" />
        </el-form-item>
        <el-form-item label="维修动作" prop="repairsAction">
          <el-input v-model="form.repairsAction" placeholder="请输入维修动作" />
        </el-form-item>
        <el-form-item label="维修时间" prop="repairsTime">
          <el-date-picker clearable
            v-model="form.repairsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择维修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="下次维修时间" prop="nextRepairsTime">
          <el-date-picker clearable
            v-model="form.nextRepairsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下次维修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修费用" prop="repairsFee">
          <el-input v-model="form.repairsFee" placeholder="请输入维修费用" />
        </el-form-item>
        <el-form-item label="备件消耗" prop="spareParts">
          <el-input v-model="form.spareParts" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修结果" prop="repairsResult">
          <el-input v-model="form.repairsResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="维修员" prop="repairsPerson">
          <el-input v-model="form.repairsPerson" placeholder="请输入维修员" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairs, getRepairs, delRepairs, addRepairs, updateRepairs } from "@/api/system/repairs";
import {listWind} from "@/api/system/wind";
import {listSet} from "@/api/system/set";
import {listEquipment} from "@/api/system/equipment";


export default {
  name: "Repairs",
  data() {
    return {
      windList: [],
      windId: '',
      setList: [],
      setId:'',
      equipmentList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修管理表格数据
      repairsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        windId: null,
        setId: null,
        equipmentId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        windId: [
          { required: true, message: "风场id不能为空", trigger: "blur" }
        ],
        setId: [
          { required: true, message: "机组id不能为空", trigger: "blur" }
        ],
        equipmentId: [
          { required: true, message: "设备id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getWindList();
    this.getSetList();
    this.getEquipmentList()
  },
  methods: {
    handleChange(val) {
      console.log(val)
      this.windId = val;
      this.getSetList()
    },
    handleSetChange(val) {
      console.log(val)
      this.setId = val;
      this.getEquipmentList()
    },
    getWindList() {
      listWind({
        pageNum: 1,
        pageSize: 10000
      }).then(response => {
        this.windList = response.rows;
      });
    },
    getSetList() {
      listSet({
        pageNum: 1,
        pageSize: 10000,
        setName: null,
        windId: this.windId
      }).then(response => {
        this.setList = response.rows;
      });
    },
    getEquipmentList() {
      listEquipment({
        pageNum: 1,
        pageSize: 10000,
        setId: this.setId,
        windId: this.windId
      }).then(response => {
        this.equipmentList = response.rows;
      });
    },
    /** 查询维修管理列表 */
    getList() {
      this.loading = true;
      listRepairs(this.queryParams).then(response => {
        this.repairsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        repairsLevel: null,
        repairsType: null,
        ticketNumber: null,
        repairsAction: null,
        repairsTime: null,
        nextRepairsTime: null,
        repairsFee: null,
        spareParts: null,
        repairsResult: null,
        windId: null,
        setId: null,
        equipmentId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        repairsPerson: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepairs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepairs(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepairs(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修管理编号为"' + ids + '"的数据项？').then(function() {
        return delRepairs(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/repairs/export', {
        ...this.queryParams
      }, `repairs_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
